﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Report;

public partial class telereport_agent_commission : System.Web.UI.Page
{
    string PageCode = "GGRPTTMAGCOMM";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
            }
            if (Session[CE.Global.General.UserRole] != null && Session[CE.Global.General.UserRole].ToString() == "200")
            {
                hplBacktoMenu.Visible = true;
            }
            else
            {
                hplBacktoMenu.Visible = false;
            }

            lblError.Text = "";
            lblContent.Text = "";
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();
            //LoadAgentList(); 
            //ddlCampaign_SelectedIndexChanged(null, null);

            int _cryr = CE.Global.General.GetUTCDateTime().Year;
            int _styr = 2010;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
                ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            ddlReportType_SelectedIndexChanged(null, null);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        txtReportType.Text = ddlReportType.Text;
        txtAgentId.Text = ddlAgentList.Text;
        LoadGrid();
    }

    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        if (ddlAgentList.Text != "0")
        {
            GenerateExcel();
        }
        else if (ddlAgentList.Text == "0")
        {
            GenerateExcelAll();
        }
        //string Campaign = "Online";
        //int CampaignId = 400;
        //if (ddlCampaign.Text == "1001")
        //{
        //    Campaign = "Offline";
        //    CampaignId = 1001;
        //}
        //if (ddlAgentList.Text != "0")
        //{
        //    GenerateExcel();
        //    if (CampaignId == 400)
        //    {
        //        GenerateExcel();
        //    }
        //    else if (CampaignId == 1001)
        //    {
        //        GenerateExcelOffline();
        //    }
        //}
        //else if (ddlAgentList.Text == "0")
        //{
        //    GenerateExcelAll();
        //    if (grvAllCommisionSummary.Rows.Count > 0)
        //    {
        //        string _strDate1 = txtReportDate1.Text;
        //        string _strDate2 = txtReportDate2.Text;
        //        string _Date = "";
        //        string _FileName = "CommissionReport@";
        //        if (ddlReportType.Text == "0")
        //        {
        //            _Date = ": " + GetMonth(_strDate1.Substring(4, 2).ToString());
        //            _Date += " " + _strDate1.Substring(0, 4).ToString();

        //            _FileName += GetMonth(_strDate1.Substring(4, 2).ToString()).Trim() + _strDate1.Substring(0, 4).ToString();
        //        }

        //        string strHeader = "<meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
        //        strHeader += "<table><tr><td colspan='8'><h3>Commission Report</h3></td>";
        //        strHeader += "<td style='color:white'>" + CE.Global.General.GetUTCDateTime() + "</td></tr>";
        //        strHeader += "<tr><td>Campaign</td><td colspan='8'>: " + Campaign + "</td></tr>";
        //        strHeader += "<tr><td>Report Month</td><td colspan='8'>: " + GetMonth(ddlMonth1.Text) + " " + ddlYear1.Text + "</td></tr>";
        //        strHeader += "<tr><td>Record Count</td><td colspan='8'>: " + grvAllCommisionSummary.Rows.Count + "</td></tr>";
        //        strHeader += "</table>";

        //        ExportGridView(_FileName, grvAllCommisionSummary, strHeader);
        //    }
        //}
    }

    protected void ddlMonth1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadAgentListByAgentId();
    }

    protected void ddlYear1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadAgentListByAgentId();
    }

    protected void ddlCampaign_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadAgentListByAgentId();
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "";
        ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = "From Date :";
            lblToDate.Text = " To Date :";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
        LoadAgentListByAgentId();
    }

    protected void grvIndAgentCommisionSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[10].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[11].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[12].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[13].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[14].Attributes.CssStyle.Add("text-align", "right");
            //if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
            //{
            //    e.Row.Cells[3].Visible = true;
            //}
            //else
            //{
            //    e.Row.Cells[3].Visible = false;
            //}
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[10].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[11].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[12].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[13].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[14].Attributes.CssStyle.Add("text-align", "right");
            //if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
            //{
            //    e.Row.Cells[4].Visible = true;
            //}
            //else
            //{
            //    e.Row.Cells[4].Visible = false;
            //}
        }
    }

    protected void grvAgentCommisionSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "center");
            e.Row.Cells[7].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            //if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
            //{
            //    e.Row.Cells[3].Visible = true;
            //}
            //else
            //{
            //    e.Row.Cells[3].Visible = false;
            //}
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "center");
            e.Row.Cells[7].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            //if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
            //{
            //    e.Row.Cells[4].Visible = true;
            //}
            //else
            //{
            //    e.Row.Cells[4].Visible = false;
            //}
        }
    }

    protected void grvCommisionSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[7].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
            {
                e.Row.Cells[3].Visible = true;
            }
            else
            {
                e.Row.Cells[3].Visible = false;
            }
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[7].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
            {
                e.Row.Cells[4].Visible = true;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
            }
        }
    }

    protected void grvAllCommisionSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[2].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[7].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[10].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[11].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[12].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[13].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[14].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[15].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[16].Attributes.CssStyle.Add("text-align", "right");      
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[2].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[7].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[8].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[9].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[10].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[11].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[12].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[13].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[14].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[15].Attributes.CssStyle.Add("text-align", "right");
            e.Row.Cells[16].Attributes.CssStyle.Add("text-align", "right");    
        }
    }

    public bool ValidDate(Int32 Year, Int32 Month, Int32 Day)
    {
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan ";
        }
        else if (month == "02")
        {
            result = " Feb ";
        }
        else if (month == "03")
        {
            result = " Mar ";
        }
        else if (month == "04")
        {
            result = " Apr ";
        }
        else if (month == "05")
        {
            result = " May ";
        }
        else if (month == "06")
        {
            result = " Jun ";
        }
        else if (month == "07")
        {
            result = " Jul ";
        }
        else if (month == "08")
        {
            result = " Aug ";
        }
        else if (month == "09")
        {
            result = " Sep ";
        }
        else if (month == "10")
        {
            result = " Oct ";
        }
        else if (month == "11")
        {
            result = " Nov ";
        }
        else if (month == "12")
        {
            result = " Dec ";
        }
        return result;
    }

    public bool Validatioin()
    {
        lblError.Text = "";
        bool _isValid = true;
        DateTime _fromDate, _toDate;
        if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            if (ddlDay1.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (_fromDate > CE.Global.General.GetUTCDateTime())
                    {
                        _isValid = false;
                        lblError.Text = "Date should be less than or equal to Today";
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = "Select Valid Date";
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = "Select Date";
            }
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            if (ddlDay1.SelectedIndex >= 0 && ddlDay2.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (ValidDate(Convert.ToInt32(ddlYear2.Text), Convert.ToInt32(ddlMonth2.Text), Convert.ToInt32(ddlDay2.Text)))
                    {
                        _toDate = Convert.ToDateTime(Convert.ToInt32(ddlYear2.Text) + "/" + Convert.ToInt32(ddlMonth2.Text) + "/" + Convert.ToInt32(ddlDay2.Text));
                        if (_fromDate <= CE.Global.General.GetUTCDateTime())
                        {
                            if (_fromDate > _toDate)
                            {
                                _isValid = false;
                                lblError.Text = "From date should be less than To date";
                            }
                        }
                        else
                        {
                            _isValid = false;
                            lblError.Text = "From date should be less than Today";
                        }
                    }
                    else
                    {
                        _isValid = false;
                        lblError.Text = "Select Valid To date";
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = "Select Valid From date";
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = "Select From date and To date";
            }
        }
        return _isValid;
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        lblCommissionTable.Text = string.Empty;
        grvCommisionSummary.Visible = false;
        grvAllCommisionSummary.Visible = false;
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            
            txtReportDate2.Text = ddlYear2.Text;
            txtReportDate2.Text += ddlMonth2.Text;
            txtReportDate2.Text += ddlDay2.Text;

            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate1 = txtReportDate1.Text.Trim() + "01";
            string _ReportDate2 = txtReportDate2.Text.Trim();
            Int64 _AgentId = Convert.ToInt64(ddlAgentList.Text);
            Int32 _UserTypeId = Convert.ToInt32(ddlCampaign.Text);

            txtReportDate.Text = _ReportDate1.ToString();


            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            SortedList _sPara = new SortedList();
            string _spName = "";

            _spName = "sprocReportsAgentCommissionSelectedList";
            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@AppType", Convert.ToInt16(ddlAppType.Text));
            _sPara.Add("@ReportDate", _ReportDate1);
            _sPara.Add("@AgentId", _AgentId);
            _sPara.Add("@UserTypeId", _UserTypeId);

            _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
            if (_AgentId > 0)
            {
                grvAgentCommisionSummary.DataSource = null;
                grvAgentCommisionSummary.DataBind();
                grvAgentCommisionSummary.Visible = false;
                if (_dtSummary != null && _dtSummary.Rows.Count > 0)
                {
                    grvIndAgentCommisionSummary.Visible = true;
                    lblError.Text = "";
                    grvIndAgentCommisionSummary.DataSource = _dtSummary;
                    grvIndAgentCommisionSummary.DataBind();
                    EnablePrintView();
                    LoadCommissionTable(txtReportDate.Text, Convert.ToInt32(ddlCampaign.Text));
                }
                else
                {
                    grvIndAgentCommisionSummary.DataSource = null;
                    grvIndAgentCommisionSummary.DataBind();
                    EnablePrintView();
                    lblCommissionTable.Text = string.Empty;
                }
            }
            else
            {
                grvIndAgentCommisionSummary.DataSource = null;
                grvIndAgentCommisionSummary.DataBind();
                grvIndAgentCommisionSummary.Visible = false;
                if (_dtSummary != null && _dtSummary.Rows.Count > 0)
                {
                    grvAgentCommisionSummary.Visible = true;
                    lblError.Text = "";
                    grvAgentCommisionSummary.DataSource = _dtSummary;
                    grvAgentCommisionSummary.DataBind();
                    EnablePrintView();
                    LoadCommissionTable(txtReportDate.Text, Convert.ToInt32(ddlCampaign.Text));
                }
                else
                {
                    grvAgentCommisionSummary.DataSource = null;
                    grvAgentCommisionSummary.DataBind();
                    EnablePrintView();
                    lblCommissionTable.Text = string.Empty;
                }
            }
            
            #region Old Reports
            /* Update on 09 Jul 2013
            if (_AgentId != 0)
            {
                grvAllCommisionSummary.DataSource = null;
                grvAllCommisionSummary.DataBind();
                grvAllCommisionSummary.Visible = false;

                string _spName = "[sprocCXCRMCompletedApplicationSelectedList]";

                _sPara.Add("@ReportType", _ReportType);
                _sPara.Add("@ReportDate", _ReportDate1);
                _sPara.Add("@AgentId", _AgentId);
                _sPara.Add("@UserTypeId", _UserTypeId);

                _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
                if (_dtSummary != null && _dtSummary.Rows.Count > 0)
                {
                    grvCommisionSummary.Visible = true;
                    lblError.Text = "";
                    grvCommisionSummary.DataSource = _dtSummary;
                    grvCommisionSummary.DataBind();
                    EnablePrintView();
                    LoadCommissionTable(txtReportDate.Text, Convert.ToInt32(ddlCampaign.Text));
                }
                else
                {
                    grvCommisionSummary.DataSource = null;
                    grvCommisionSummary.DataBind();
                    EnablePrintView();
                    lblCommissionTable.Text = string.Empty;
                }
            }
            else if (_AgentId == 0)
            {
                grvCommisionSummary.DataSource = null;
                grvCommisionSummary.DataBind();
                grvCommisionSummary.Visible = false;
                if (_ReportDate1.Length == 6)
                {
                    _ReportDate1 = _ReportDate1.ToString() + "01";
                }
                string _spName = "[sprocCXCRMAllAgentCommissionSelectedList]";
                _sPara.Add("@ReportDate", _ReportDate1);
                _sPara.Add("@UserTypeId", _UserTypeId);

                _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
                if (_dtSummary != null && _dtSummary.Rows.Count > 0)
                {
                    grvAllCommisionSummary.Visible = true;
                    lblError.Text = "";
                    grvAllCommisionSummary.DataSource = _dtSummary;
                    grvAllCommisionSummary.DataBind();
                    EnablePrintView();
                    LoadCommissionTable(txtReportDate.Text, Convert.ToInt32(ddlCampaign.Text));
                }
                else
                {
                    grvAllCommisionSummary.DataSource = null;
                    grvAllCommisionSummary.DataBind();
                    EnablePrintView();
                    lblCommissionTable.Text = string.Empty;
                }
            }*/
            #endregion
        }
        else
        {
            grvIndAgentCommisionSummary.DataSource = null;
            grvIndAgentCommisionSummary.DataBind(); 
            grvAgentCommisionSummary.DataSource = null;
            grvAgentCommisionSummary.DataBind();           
            EnablePrintView();
            lblCommissionTable.Text = string.Empty;
        }
    }

    public void LoadCommissionTable(string ReportDate, Int32 UserTypeId)
    {
        string _strCommissionTable = "";
        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        string _spName = "[sprocCXCRMAgentCommissionSelectItemList]";
        SortedList _sPara = new SortedList();

        _sPara.Add("@ReportDate", ReportDate);
        _sPara.Add("@UserTypeId", UserTypeId);
        DataTable _dtCommission = new DataTable();
        _dtCommission = _db.GetDataTableBySP(_spName, _sPara);
        if (_dtCommission != null && _dtCommission.Rows.Count > 0)
        {
            if (UserTypeId == 400)
            {
                _strCommissionTable = "<table cellpadding='1' cellspacing='1' border='1' width='30%'>";
                _strCommissionTable += "<tr>";
                _strCommissionTable += "<td style='width:180px; font-weight:bold; text-align:center;' colspan='2'>Premium Collected (" + GetReportMonth(ReportDate.Substring(0, 6)) + ")</td>";
                _strCommissionTable += "<td style='width:120px; font-weight:bold; text-align:center;'>% of commission</td>";
                _strCommissionTable += "</tr>";
                for (int i = 0; i < _dtCommission.Rows.Count; i++)
                {
                    int _PremiumStart = 0;
                    int _PremiumEnd = 0;
                    float _Commissions = 0;
                    if (!int.TryParse(_dtCommission.Rows[i]["PremiumStart"].ToString(), out _PremiumStart))
                    {
                        _PremiumStart = 0;
                    }
                    if (!int.TryParse(_dtCommission.Rows[i]["PremiumEnd"].ToString(), out _PremiumEnd))
                    {
                        _PremiumEnd = 0;
                    }
                    if (!float.TryParse(_dtCommission.Rows[i]["Commissions"].ToString(), out _Commissions))
                    {
                        _Commissions = 0;
                    }
                    _strCommissionTable += "<tr>";
                    if (_PremiumStart > 1)
                    {
                        _strCommissionTable += "<td style='text-align:right;'>" + _PremiumStart.ToString("#,000") + "</td>";
                    }
                    else
                    {
                        _strCommissionTable += "<td style='text-align:right;'>" + _PremiumStart.ToString("#") + "</td>";
                    }
                    if (_PremiumEnd > 0)
                    {
                        _strCommissionTable += "<td style='text-align:right;'>" + _PremiumEnd.ToString("#,000") + "</td>";
                    }
                    else
                    {
                        _strCommissionTable += "<td style='text-align:right;'>ABOVE</td>";
                    }
                    _strCommissionTable += "<td style='text-align:center;'>" + _Commissions.ToString("#.0") + "%</td>";
                    _strCommissionTable += "</tr>";
                }
                _strCommissionTable += "</table>";
            }
            else if (UserTypeId == 1001)
            {
                _strCommissionTable = "<table cellpadding='1' cellspacing='1' border='1' width='35%'>";
                _strCommissionTable += "<tr>";
                _strCommissionTable += "<td style='width:80px; font-weight:bold; text-align:center;'>Class</td>";
                _strCommissionTable += "<td style='width:180px; font-weight:bold; text-align:center;' colspan='2'>Premium Collected (" + GetReportMonth(ReportDate.Substring(0, 6)) + ")</td>";
                _strCommissionTable += "<td style='width:120px; font-weight:bold; text-align:center;'>Commission / Car</td>";
                _strCommissionTable += "</tr>";
                for (int i = 0; i < _dtCommission.Rows.Count; i++)
                {
                    string _Class = " ";
                    int _PremiumStart = 0;
                    int _PremiumEnd = 0;
                    float _Commissions = 0;
                    _Class = _dtCommission.Rows[i]["Class"].ToString();
                    if (!int.TryParse(_dtCommission.Rows[i]["PremiumStart"].ToString(), out _PremiumStart))
                    {
                        _PremiumStart = 0;
                    }
                    if (!int.TryParse(_dtCommission.Rows[i]["PremiumEnd"].ToString(), out _PremiumEnd))
                    {
                        _PremiumEnd = 0;
                    }
                    if (!float.TryParse(_dtCommission.Rows[i]["Commissions"].ToString(), out _Commissions))
                    {
                        _Commissions = 0;
                    }

                    _strCommissionTable += "<tr>";
                    if (_Class != "0")
                    {
                        _strCommissionTable += "<td style='text-align:center;'>Class " + _Class + "</td>";
                    }
                    else if (_Class == "0")
                    {
                        _strCommissionTable += "<td style='text-align:center;'>All VMI</td>";
                    }

                    if (_PremiumStart > 1)
                    {
                        _strCommissionTable += "<td style='text-align:right;'>" + _PremiumStart.ToString("#,000") + "</td>";
                    }
                    else
                    {
                        _strCommissionTable += "<td style='text-align:right;'>" + _PremiumStart.ToString("#") + "</td>";
                    }
                    if (_PremiumEnd > 0)
                    {
                        _strCommissionTable += "<td style='text-align:right;'>" + _PremiumEnd.ToString("#,000") + "</td>";
                    }
                    else
                    {
                        _strCommissionTable += "<td style='text-align:right;'>ABOVE</td>";
                    }
                    if (_Commissions < 100)
                    {
                        _strCommissionTable += "<td style='text-align:center;'>" + _Commissions.ToString() + "</td>";
                    }
                    else
                    {
                        _strCommissionTable += "<td style='text-align:center;'>" + _Commissions.ToString("#,000") + "</td>";
                    }
                    _strCommissionTable += "</tr>";
                }
                _strCommissionTable += "</table>";
            }
            lblCommissionTable.Text = _strCommissionTable.ToString();
        }
    }

    private void EnablePrintView()
    {
        if (grvIndAgentCommisionSummary.Rows.Count > 0)
        {
            ddlLanguage.Visible = false;
            btnDownLoad.Visible = true;
            lblRecordCount.Text = "(" + grvCommisionSummary.Rows.Count.ToString() + ") record(s). Date/Time: " + CE.Global.General.GetUTCDateTime().ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
        else if (grvAgentCommisionSummary.Rows.Count > 0)
        {
            ddlLanguage.Visible = false;
            btnDownLoad.Visible = true;
            lblRecordCount.Text = "(" + grvAllCommisionSummary.Rows.Count.ToString() + ") record(s). Date/Time: " + CE.Global.General.GetUTCDateTime().ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
        else
        {
            ddlLanguage.Visible = false;
            btnDownLoad.Visible = false;
            lblRecordCount.Text = "";
        }
    }

    public void GenerateExcel()
    {
        string _html = "";
        string _fileName = "Commision";
        lblContent.Text = "";
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }


            Int32 CommissionCount = 0;
            Int32 TotalFileCount = 0;
            DataSet _dsSummary = new DataSet();
            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate = txtReportDate.Text.Trim();
            Int64 _AgentId = Convert.ToInt64(ddlAgentList.Text);
            Int32 _UserTypeId = Convert.ToInt32(ddlCampaign.Text);

            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocReportsAgentCommissionSelectedList";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@AppType", Convert.ToInt16(ddlAppType.Text));
            _sPara.Add("@ReportDate", _ReportDate);
            _sPara.Add("@AgentId", _AgentId);
            _sPara.Add("@UserTypeId", _UserTypeId);

            _dsSummary = _db.GetDataSet(_spName, _sPara);
            if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
            {
                TotalFileCount += _dsSummary.Tables[0].Rows.Count;
            }
            if (TotalFileCount > 0)
            {
                string[] AgentId = new string[TotalFileCount];
                string[] AgentName = new string[TotalFileCount];
                string[] OrderDate = new string[TotalFileCount];
                string[] TypeOfLeads = new string[TotalFileCount];
                string[] ApplicationName = new string[TotalFileCount];
                string[] Insurer = new string[TotalFileCount];
                string[] InsuranceType = new string[TotalFileCount];
                string[] CarPlate = new string[TotalFileCount];
                string[] QuoteRefNo = new string[TotalFileCount];
                string[] VMI = new string[TotalFileCount];
                string[] CMI = new string[TotalFileCount];
                string[] CollectedAmount = new string[TotalFileCount];
                string[] TotalAmount = new string[TotalFileCount];
                string[] CommissionPer = new string[TotalFileCount];
                string[] Commission = new string[TotalFileCount];
                string[] Incentive = new string[TotalFileCount];
                string[] Total = new string[TotalFileCount];
                string[] Remarks = new string[TotalFileCount];


                decimal _dCommPer = 0;
                decimal _dVMI = 0;
                decimal _dCMI = 0;
                decimal _dCommission = 0;
                decimal _dIncentive = 0;

                decimal dTotalVMI = 0;
                decimal dTotalCMI = 0;
                decimal dTotalCommission = 0;
                decimal dTotalIncentive = 0;
                decimal dTotalCommissionIncentive = 0;
                for (int i = 0; i < TotalFileCount; i++)
                {
                    AgentId[i] = _dsSummary.Tables[0].Rows[i]["AgentId"].ToString();
                    AgentName[i] = _dsSummary.Tables[0].Rows[i]["AgentName"].ToString();
                    OrderDate[i] = _dsSummary.Tables[0].Rows[i]["OrderDate"].ToString();
                    TypeOfLeads[i] = _dsSummary.Tables[0].Rows[i]["TypeOfLeads"].ToString();
                    ApplicationName[i] = _dsSummary.Tables[0].Rows[i]["ApplicationName"].ToString();
                    Insurer[i] = _dsSummary.Tables[0].Rows[i]["Insurer"].ToString();
                    InsuranceType[i] = _dsSummary.Tables[0].Rows[i]["InsuranceType"].ToString();
                    CarPlate[i] = _dsSummary.Tables[0].Rows[i]["CarPlate"].ToString();
                    QuoteRefNo[i] = _dsSummary.Tables[0].Rows[i]["QuoteRefNo"].ToString();
                    VMI[i] = _dsSummary.Tables[0].Rows[i]["VMI"].ToString();
                    CMI[i] = _dsSummary.Tables[0].Rows[i]["CMI"].ToString();
                    CollectedAmount[i] = _dsSummary.Tables[0].Rows[i]["CollectedAmount"].ToString();
                    TotalAmount[i] = _dsSummary.Tables[0].Rows[i]["TotalAmount"].ToString();
                    CommissionPer[i] = _dsSummary.Tables[0].Rows[i]["CommissionPer"].ToString();
                    Commission[i] = _dsSummary.Tables[0].Rows[i]["Commission"].ToString();
                    Incentive[i] = _dsSummary.Tables[0].Rows[i]["Incentive"].ToString();
                    Total[i] = _dsSummary.Tables[0].Rows[i]["Total"].ToString();
                    Remarks[i] = " ";
                    if (!decimal.TryParse(CommissionPer[i].ToString(), out _dCommPer))
                    {
                        _dCommPer = 0;
                    }
                    if (_dCommPer > 0 && _dCommPer < 10)
                    {
                        CommissionPer[i] = _dsSummary.Tables[0].Rows[i]["CommissionPer"].ToString() + "%";
                    }
                    if (!decimal.TryParse(VMI[i].ToString(), out _dVMI))
                    {
                        _dVMI = 0;
                    }
                    dTotalVMI += _dVMI;
                    if (!decimal.TryParse(CMI[i].ToString(), out _dCMI))
                    {
                        _dCMI = 0;
                    }
                    dTotalCMI += _dCMI;
                    if (!decimal.TryParse(Commission[i].ToString(), out _dCommission))
                    {
                        _dCommission = 0;
                    }
                    dTotalCommission += _dCommission;
                    if (!decimal.TryParse(Incentive[i].ToString(), out _dIncentive))
                    {
                        _dIncentive = 0;
                    }
                    dTotalIncentive += _dIncentive;
                }
                dTotalCommissionIncentive = dTotalCommission + dTotalIncentive;
                DateTime _RptDate = CE.Global.General.GetUTCDateTime();
                if (!DateTime.TryParse(OrderDate[0].ToString(), out _RptDate))
                {
                    _RptDate = CE.Global.General.GetUTCDateTime();
                }
                _fileName = AgentId[0].ToString() + "_" + _fileName + "@" + _RptDate.ToString("MMMyyyy");
                _html += "<html>";
                _html += "<head>";
                _html += "<meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
                _html += "</head>";
                _html += "<body>";

                if (ddlLanguage.Text == "1" || ddlLanguage.Text == "2")
                {
                    _html += "<table width='100%' cellpadding='1' cellspacing='1'>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Agent Name</b></td>";
                    _html += "<td align='left' colspan='15' style='font-size:14px; height:30px;'><b>" + AgentName[0].ToString() + "</b></td>";
                    _html += "</tr>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Project</b></td>";
                    _html += "<td align='left' colspan='15' style='font-size:14px; height:30px;'><b>GG</b></td>";
                    _html += "</tr>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Period</b></td>";
                    _html += "<td align='left' colspan='15' style='font-size:14px; height:30px; mso-number-format:mmm\\-yyyy;'><b>" + OrderDate[0].ToString() + "</b></td>";
                    _html += "</tr>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Report</b></td>";
                    _html += "<td align='left' colspan='15' style='font-size:14px; height:30px;'><b>Agent Commission</b></td>";
                    _html += "</tr>";
                    _html += "</table>";

                    _html += "<table width='100%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr>";
                    _html += "<td width='100' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Order Date</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Type Of Leads</b></td>";
                    _html += "<td width='200' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Application Name</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Insurer</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Insurance Type</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Car Plate</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>QuoteRefNo</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>VMI</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>CMI</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>CollectedAmount</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>TotalAmount</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Commission Tier</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Commission</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Incentive</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Total</b></td>";
                    _html += "<td width='120' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Remarks</b></td>";
                    _html += "</tr>";
                    
                    //Broker Commission Summary - Start
                    #region Broker Commission
                    for (int j = 0; j < TotalFileCount; j++)
                    {
                        _html += "<tr>";
                        _html += "<td width='100' height='30' style='text-align: center; vertical-align:middle; font-size:12px; mso-number-format:dd\\-MMM\\-yyyy;'>" + OrderDate[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + TypeOfLeads[j].ToString() + "</td>";
                        _html += "<td width='200' height='30' style='text-align: left; vertical-align:middle; font-size:12px;'>" + ApplicationName[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + Insurer[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + InsuranceType[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + CarPlate[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + QuoteRefNo[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + VMI[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + CMI[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00; background-color:#C7FFE6;'>" + CollectedAmount[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00; background-color:#C7FFE6;'>" + TotalAmount[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + CommissionPer[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + Commission[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + Incentive[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + Total[j].ToString() + "</td>";
                        _html += "<td width='120' height='30' style='text-align: left; vertical-align:middle; font-size:12px;'>" + Remarks[j].ToString() + "</td>";
                        _html += "</tr>";
                    }
                    _html += "<tr>";
                    _html += "<td colspan='7' style='font-size:14px; background-color:#FFE100;'>&nbsp;</td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalVMI.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalCMI.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FF0000; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + TotalAmount[0].ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + TotalAmount[0].ToString() + "</b></td>";
                    _html += "<td width='80px' align='center' style='font-size:14px; background-color:#FFE100;'><b>" + CommissionPer[0].ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalCommission.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalIncentive.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FF0000; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalCommissionIncentive.ToString() + "</b></td>";
                    _html += "<td style='font-size:14px; background-color:#FFE100;'>&nbsp;</td>";
                    _html += "</tr>";
                    
                    _html += "</table>";                   
                    #endregion
                }
                _html += "</body>";
                _html += "</html>";


                lblContent.Text = _html;
                ExportGridView(_fileName, lblContent);
            }
        }
    }

    public void GenerateExcelAll()
    {
        string _html = "";
        string _fileName = "AgentsCommision";
        lblContent.Text = "";
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }


            Int32 CommissionCount = 0;
            Int32 TotalFileCount = 0;
            DataSet _dsSummary = new DataSet();
            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate = txtReportDate.Text.Trim();
            Int64 _AgentId = Convert.ToInt64(ddlAgentList.Text);
            Int32 _UserTypeId = Convert.ToInt32(ddlCampaign.Text);

            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocReportsAgentCommissionSelectedList";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@AppType", Convert.ToInt16(ddlAppType.Text));
            _sPara.Add("@ReportDate", _ReportDate);
            _sPara.Add("@AgentId", _AgentId);
            _sPara.Add("@UserTypeId", _UserTypeId);

            _dsSummary = _db.GetDataSet(_spName, _sPara);
            if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
            {
                TotalFileCount += _dsSummary.Tables[0].Rows.Count;
            }
            if (TotalFileCount > 0)
            {
                string[] AgentId = new string[TotalFileCount];
                string[] BrokerName = new string[TotalFileCount];
                string[] AgentNameTH = new string[TotalFileCount];
                string[] AgentNameEN = new string[TotalFileCount];
                string[] VMI = new string[TotalFileCount];
                string[] CMI = new string[TotalFileCount];
                string[] CollectedAmount = new string[TotalFileCount];
                string[] CommissionPer = new string[TotalFileCount];
                string[] Commission = new string[TotalFileCount];
                string[] Incentive = new string[TotalFileCount];
                string[] Total = new string[TotalFileCount];


                decimal _dCommPer = 0;
                decimal _dVMI = 0;
                decimal _dCMI = 0;
                decimal _dColloected = 0;
                decimal _dCommission = 0;
                decimal _dIncentive = 0;

                decimal dTotalVMI = 0;
                decimal dTotalCMI = 0;
                decimal dTotalColloected = 0;
                decimal dTotalCommission = 0;
                decimal dTotalIncentive = 0;
                decimal dTotalCommissionIncentive = 0;
                for (int i = 0; i < TotalFileCount; i++)
                {
                    AgentId[i] = _dsSummary.Tables[0].Rows[i]["AgentId"].ToString();
                    BrokerName[i] = _dsSummary.Tables[0].Rows[i]["BrokerName"].ToString();
                    AgentNameTH[i] = _dsSummary.Tables[0].Rows[i]["AgentNameTH"].ToString();
                    AgentNameEN[i] = _dsSummary.Tables[0].Rows[i]["AgentNameEN"].ToString();
                    VMI[i] = _dsSummary.Tables[0].Rows[i]["VMI"].ToString();
                    CMI[i] = _dsSummary.Tables[0].Rows[i]["CMI"].ToString();
                    CollectedAmount[i] = _dsSummary.Tables[0].Rows[i]["CollectedAmount"].ToString();
                    CommissionPer[i] = _dsSummary.Tables[0].Rows[i]["CommissionPer"].ToString();
                    Commission[i] = _dsSummary.Tables[0].Rows[i]["Commission"].ToString();
                    Incentive[i] = _dsSummary.Tables[0].Rows[i]["Incentive"].ToString();
                    Total[i] = _dsSummary.Tables[0].Rows[i]["Total"].ToString();
                    if (!decimal.TryParse(CommissionPer[i].ToString(), out _dCommPer))
                    {
                        _dCommPer = 0;
                    }
                    if (_dCommPer > 0 && _dCommPer < 10)
                    {
                        CommissionPer[i] = _dsSummary.Tables[0].Rows[i]["CommissionPer"].ToString() + "%";
                    }
                    if (!decimal.TryParse(VMI[i].ToString(), out _dVMI))
                    {
                        _dVMI = 0;
                    }
                    dTotalVMI += _dVMI;
                    if (!decimal.TryParse(CMI[i].ToString(), out _dCMI))
                    {
                        _dCMI = 0;
                    }
                    dTotalCMI += _dCMI;
                    if (!decimal.TryParse(CollectedAmount[i].ToString(), out _dColloected))
                    {
                        _dColloected = 0;
                    }
                    dTotalColloected += _dColloected;
                    if (!decimal.TryParse(Commission[i].ToString(), out _dCommission))
                    {
                        _dCommission = 0;
                    }
                    dTotalCommission += _dCommission;
                    if (!decimal.TryParse(Incentive[i].ToString(), out _dIncentive))
                    {
                        _dIncentive = 0;
                    }
                    dTotalIncentive += _dIncentive;
                }
                dTotalCommissionIncentive = dTotalCommission + dTotalIncentive;

                _fileName = _fileName + "@" + GetMonth(ddlMonth1.Text).Trim() + ddlYear1.Text;
                _html += "<html>";
                _html += "<head>";
                _html += "<meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
                _html += "</head>";
                _html += "<body>";

                if (ddlLanguage.Text == "1" || ddlLanguage.Text == "2")
                {
                    _html += "<table width='100%' cellpadding='1' cellspacing='1'>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Project</b></td>";
                    _html += "<td align='left' colspan='8' style='font-size:14px; height:30px;'><b>GG</b></td>";
                    _html += "</tr>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Period</b></td>";
                    _html += "<td align='left' colspan='8' style='font-size:14px; height:30px;'><b>" + GetMonth(ddlMonth1.Text) + ddlYear1.Text +"</b></td>";
                    _html += "</tr>";

                    _html += "<tr>";
                    _html += "<td align='left' style='font-size:14px; width:100px; height:30px;'><b>Report</b></td>";
                    _html += "<td align='left' colspan='8' style='font-size:14px; height:30px;'><b>Agents Commission</b></td>";
                    _html += "</tr>";
                    _html += "</table>";

                    _html += "<table width='100%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr>";
                    _html += "<td width='100' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Broker</b></td>";
                    _html += "<td width='150' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Agent Name</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>VMI</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>CMI</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>CollectedAmount</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Commission Tier</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Commission</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Incentive</b></td>";
                    _html += "<td width='80' height='40' style='text-align: center; vertical-align:middle; font-size:14px; background-color:#808080; color:#FFFFFF;'><b>Total</b></td>";
                    _html += "</tr>";

                    #region Broker Commission
                    for (int j = 0; j < TotalFileCount; j++)
                    {
                        _html += "<tr>";
                        _html += "<td width='100' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + BrokerName[j].ToString() + "</td>";
                        _html += "<td width='120' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + AgentNameEN[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + VMI[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + CMI[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00; background-color:#C7FFE6;'>" + CollectedAmount[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: center; vertical-align:middle; font-size:12px;'>" + CommissionPer[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + Commission[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + Incentive[j].ToString() + "</td>";
                        _html += "<td width='80' height='30' style='text-align: right; vertical-align:middle; font-size:12px; mso-number-format:\\#\\,\\#\\#0\\.00;'>" + Total[j].ToString() + "</td>";
                        _html += "</tr>";
                    }
                    _html += "<tr>";
                    _html += "<td colspan='2' style='font-size:14px; background-color:#FFE100;'>&nbsp;</td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalVMI.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalCMI.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FF0000; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalColloected.ToString() + "</b></td>"; 
                    _html += "<td width='80px' align='center' style='font-size:14px; background-color:#FFE100;'><b>&nbsp;</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalCommission.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalIncentive.ToString() + "</b></td>";
                    _html += "<td width='80px' align='right' style='font-size:14px; background-color:#FF0000; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + dTotalCommissionIncentive.ToString() + "</b></td>";
                    _html += "</tr>";

                    _html += "</table>";
                    #endregion
                }
                _html += "</body>";
                _html += "</html>";


                lblContent.Text = _html;
                ExportGridView(_fileName, lblContent);
            }
        }
    }

    public void GenerateExcelOld()
    {
        string _html = "";
        string _fileName = "AgentCommision@" + CE.Global.General.GetUTCDateTime().Day.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Month.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Year;
        lblContent.Text = "";
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }


            Int32 CommissionCount = 0;
            Int32 TotalFileCount = 0;
            DataSet _dsSummary = new DataSet();
            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate = txtReportDate.Text.Trim();
            Int64 AgentId = Convert.ToInt64(ddlAgentList.Text);
            Int32 _UserTypeId = Convert.ToInt32(ddlCampaign.Text);

            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "[sprocCXCRMCompletedApplicationSelectedList]";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@ReportDate", _ReportDate);
            _sPara.Add("@AgentId", AgentId);
            _sPara.Add("@UserTypeId", _UserTypeId);

            _dsSummary = _db.GetDataSet(_spName, _sPara);
            if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
            {
                TotalFileCount += _dsSummary.Tables[0].Rows.Count;
            }
            if (TotalFileCount > 0)
            {
                string[] ApplicationDate = new string[TotalFileCount];
                string[] QuoteRefNo = new string[TotalFileCount];
                string[] AgentName = new string[TotalFileCount];
                string[] CustomerName = new string[TotalFileCount];
                string[] InsuranceType = new string[TotalFileCount];
                string[] CompanyName = new string[TotalFileCount];
                string[] VMI = new string[TotalFileCount];
                string[] CMI = new string[TotalFileCount];
                string[] Premium = new string[TotalFileCount];
                string[] CollectedAmount = new string[TotalFileCount];
                float TotalCollectedPremium = 0.0f;
                string[] Agent = new string[0];
                string[] CollectedPremium = new string[0];
                string[] Percentage = new string[0];
                string[] AgentCommission = new string[0];
                float CollectedPremiumTotal = 0.0f;
                float AgentCommissionTotal = 0.0f;


                string SCName, SCBank, CardNo, CardType, CardExpiry, Status, Commision, TotalCommision, PaymentType;
                SCName = ""; SCBank = ""; CardNo = ""; CardType = ""; CardExpiry = ""; Status = ""; Commision = ""; TotalCommision = "";
                PaymentType = "";
                for (int i = 0; i < TotalFileCount; i++)
                {
                    ApplicationDate[i] = Convert.ToDateTime(_dsSummary.Tables[0].Rows[i]["ApplicationDate"].ToString()).ToString("dd/MM/yyyy hh:mm tt");
                    QuoteRefNo[i] = _dsSummary.Tables[0].Rows[i]["QuoteRefNo"].ToString();
                    AgentName[i] = _dsSummary.Tables[0].Rows[i]["AgentName"].ToString();
                    CustomerName[i] = _dsSummary.Tables[0].Rows[i]["CustomerName"].ToString();
                    InsuranceType[i] = _dsSummary.Tables[0].Rows[i]["InsuranceType"].ToString();
                    CompanyName[i] = _dsSummary.Tables[0].Rows[i]["CompanyName"].ToString();
                    VMI[i] = _dsSummary.Tables[0].Rows[i]["VMI"].ToString();
                    CMI[i] = _dsSummary.Tables[0].Rows[i]["CMI"].ToString();
                    Premium[i] = _dsSummary.Tables[0].Rows[i]["TotalPremium"].ToString();
                    CollectedAmount[i] = _dsSummary.Tables[0].Rows[i]["CollectedAmount"].ToString();
                    TotalCollectedPremium += Convert.ToSingle(CollectedAmount[i].ToString());
                }
                if (_dsSummary != null && _dsSummary.Tables.Count > 1 && _dsSummary.Tables[1].Rows.Count > 0)
                {
                    CommissionCount = _dsSummary.Tables[1].Rows.Count;
                    Agent = new string[CommissionCount];
                    CollectedPremium = new string[CommissionCount];
                    Percentage = new string[CommissionCount];
                    AgentCommission = new string[CommissionCount];
                    for (int a = 0; a < CommissionCount; a++)
                    {
                        Agent[a] = _dsSummary.Tables[1].Rows[a]["AgentName"].ToString();
                        CollectedPremium[a] = _dsSummary.Tables[1].Rows[a]["CollectedAmount"].ToString();
                        if (CollectedPremium[a] != null && CollectedPremium[a].ToString() != string.Empty && CollectedPremium[a].ToString() != "0.00" && CollectedPremium[a].ToString() != "0")
                        {
                            DataTable _dtPer = new DataTable();
                            float _AgentPremium = 0.0f;
                            //_AgentPremium = Convert.ToSingle(_dsSummary.Tables[1].Rows[a]["CollectedAmount"].ToString());                        
                            if (!float.TryParse(_dsSummary.Tables[1].Rows[a]["CollectedAmount"].ToString(), out _AgentPremium))
                            {
                                _AgentPremium = 0.0f;
                            }

                            Int32 _intRptDate = 20120101;
                            Int32 _intEffDate = 20121001;
                            DateTime _dtCurDate = new DateTime(2012, 10, 01);
                            string _strRptDate = txtReportDate.Text.Trim();
                            if (!Int32.TryParse(_strRptDate.ToString(), out _intRptDate))
                            {
                                _intRptDate = 20120101;
                            }

                            if (_intRptDate >= _intEffDate)
                            {
                                _dtPer = GetPercentage("0", txtReportDate.Text.Trim(), _AgentPremium);
                                if (_dtPer != null && _dtPer.Rows.Count > 0)
                                {
                                    Commision = _dtPer.Rows[0]["Commission"].ToString();
                                }
                                else
                                {
                                    Commision = "0.0";
                                }
                            }
                            else
                            {
                                if (_AgentPremium >= 550000)
                                {
                                    _dtPer = GetPercentage("550000", txtReportDate.Text.Trim());
                                    if (_dtPer != null && _dtPer.Rows.Count > 0)
                                    {
                                        Commision = _dtPer.Rows[0]["Commission"].ToString();
                                    }
                                    else
                                    {
                                        Commision = "0.0";
                                    }
                                }
                                else if (_AgentPremium >= 500000 && _AgentPremium < 550000)
                                {
                                    _dtPer = GetPercentage("500000", txtReportDate.Text.Trim());
                                    if (_dtPer != null && _dtPer.Rows.Count > 0)
                                    {
                                        Commision = _dtPer.Rows[0]["Commission"].ToString();
                                    }
                                    else
                                    {
                                        Commision = "0.0";
                                    }
                                }
                                else if (_AgentPremium >= 450000 && _AgentPremium < 500000)
                                {
                                    _dtPer = GetPercentage("450000", txtReportDate.Text.Trim());
                                    if (_dtPer != null && _dtPer.Rows.Count > 0)
                                    {
                                        Commision = _dtPer.Rows[0]["Commission"].ToString();
                                    }
                                    else
                                    {
                                        Commision = "0.0";
                                    }
                                }
                                else if (_AgentPremium >= 400000 && _AgentPremium < 450000)
                                {
                                    _dtPer = GetPercentage("400000", txtReportDate.Text.Trim());
                                    if (_dtPer != null && _dtPer.Rows.Count > 0)
                                    {
                                        Commision = _dtPer.Rows[0]["Commission"].ToString();
                                    }
                                    else
                                    {
                                        Commision = "0.0";
                                    }
                                }
                                else if (_AgentPremium >= 300000 && _AgentPremium < 400000)
                                {
                                    _dtPer = GetPercentage("300000", txtReportDate.Text.Trim());
                                    if (_dtPer != null && _dtPer.Rows.Count > 0)
                                    {
                                        Commision = _dtPer.Rows[0]["Commission"].ToString();
                                    }
                                    else
                                    {
                                        Commision = "0.0";
                                    }
                                }
                                else if (_AgentPremium < 300000)
                                {
                                    _dtPer = GetPercentage("0", txtReportDate.Text.Trim());
                                    if (_dtPer != null && _dtPer.Rows.Count > 0)
                                    {
                                        Commision = _dtPer.Rows[0]["Commission"].ToString();
                                    }
                                    else
                                    {
                                        Commision = "0.0";
                                    }
                                }
                            }
                            float _comper = Convert.ToSingle(Commision.ToString()) * 100;
                            float _com = Convert.ToSingle(Commision.ToString());
                            Percentage[a] = _comper.ToString() + "%";
                            AgentCommission[a] = (_AgentPremium * _com).ToString();
                            CollectedPremiumTotal += _AgentPremium;
                            AgentCommissionTotal += (_AgentPremium * _com);
                        }
                        else
                        {
                            Percentage[a] = "0.0%";
                            AgentCommission[a] = "0.00";
                        }
                    }
                }

                _html += "<html>";
                _html += "<head>";
                _html += "<meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
                _html += "</head>";
                _html += "<body>";

                if (ddlLanguage.Text == "1")
                {
                    _html += "<table width='100%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr>";

                    //Broker Commission Summary - Start
                    #region Broker Commission
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='4' style='font-size:20px; background-color:#D8D8D8;'><b>คอมมิชชั่นสำหรับโบรกเกอร์</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>โบรกเกอร์</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>จํานวนเงินที่เก็บรวบรวมโดยรวม</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>คอม(%)</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>คอมมิชชั่น</b></td>";
                    _html += "</tr>";

                    for (int b = 0; b < CommissionCount; b++)
                    {
                        _html += "<tr>";
                        _html += "<td align='left'>" + Agent[b].ToString() + "</td>";
                        _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedPremium[b].ToString() + "</td>";
                        _html += "<td align='center'>" + Percentage[b].ToString() + "</td>";
                        _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentCommission[b].ToString() + "</td>";
                        _html += "</tr>";
                    }

                    _html += "<tr>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100;'><b>&nbsp;</b></td>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedPremiumTotal.ToString() + "</td>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100;'><b>&nbsp;</b></td>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentCommissionTotal.ToString() + "</td>";
                    _html += "</tr>";

                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Broker Commission Summary - End

                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table></td>";
                    }
                    else
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td></tr></table></td>";
                    }

                    //Report Details - Start
                    #region Report Details
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='4' style='font-size:20px; background-color:#D8D8D8;'><b>รายละเอียดรายงาน</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>ผู้ใช้งาน</b></td>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>" + Session[CE.Global.General.UserName].ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>เดือนที่เลือก</b></td>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF; mso-number-format:mmm\\,yyyy'><b>" + GetReportMonth(txtReportDate.Text.Trim()) + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>รายงานวันที่ / เวลา</b></td>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'><b>" + CE.Global.General.GetUTCDateTime() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Report Details - End

                    _html += "</tr>";
                    _html += "</table>";

                    _html += "<table width='20%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "</table>";

                    //Agent submission - Start
                    #region Agent submission
                    _html += "<table width='100%' cellpadding='1' cellspacing='1' border='1'>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<tr><td align='left' colspan='10' style='font-size:20px; background-color:#D8D8D8;'><b>รายละเอียดการทำรายการ</b></td></tr>";
                    }
                    else
                    {
                        _html += "<tr><td align='left' colspan='9' style='font-size:20px; background-color:#D8D8D8;'><b>รายละเอียดการทำรายการ</b></td></tr>";
                    }
                    _html += "<tr>";
                    _html += "<td width='120' align='left' style='font-size:14px; background-color:#81BEF7;'><b>วันที่สมัคร</b></td>";
                    _html += "<td width='100' align='left' style='font-size:14px; background-color:#81BEF7;'><b>หมายเลขอ้างอิง</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>โบรกเกอร์</b></td>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>ชื่อลูกค้า</b></td>";
                    }
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>ประเภทประกันภัย</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>บริษัทประกันภัย</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>ประกันภัยรถยนต์</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>พ.ร.บ</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>เบี้ยรวมทั้งหมด</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>จำนวนเงินที่รวบรวมได้</b></td>";
                    _html += "</tr>";
                    for (int j = 0; j < _dsSummary.Tables[0].Rows.Count; j++)
                    {

                        _html += "<tr>";
                        _html += "<td width='120' align='left' style='background-color:#FBEFFB; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'>" + ApplicationDate[j].ToString() + "</td>";
                        _html += "<td width='100' align='left' style='background-color:#FBEFFB;'>" + QuoteRefNo[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + AgentName[j].ToString() + "</td>";
                        if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                        {
                            _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CustomerName[j].ToString() + "</td>";
                        }
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + InsuranceType[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CompanyName[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + VMI[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CMI[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + Premium[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedAmount[j].ToString() + "</td>";
                        _html += "</tr>";

                    }
                    _html += "<tr>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td colspan='9' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>จํานวนเงินที่เก็บรวบรวมโดยรวม</b></td>";
                    }
                    else
                    {
                        _html += "<td colspan='8' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>จํานวนเงินที่เก็บรวบรวมโดยรวม</b></td>";
                    }
                    _html += "<td width='100px' align='right' style='font-size:17px; background-color:#F5FBEF; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + TotalCollectedPremium.ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    #endregion
                    //Agent submission - End
                }
                else if (ddlLanguage.Text == "2")
                {
                    _html += "<table width='100%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr>";

                    //Broker Commission Summary - Start
                    #region Broker Commission
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='4' style='font-size:20px; background-color:#D8D8D8;'><b>Broker Commission</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>Broker</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>Total Collected Amount</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>Com(%)</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>Commission</b></td>";
                    _html += "</tr>";

                    for (int b = 0; b < CommissionCount; b++)
                    {
                        _html += "<tr>";
                        _html += "<td align='left'>" + Agent[b].ToString() + "</td>";
                        _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedPremium[b].ToString() + "</td>";
                        _html += "<td align='center'>" + Percentage[b].ToString() + "</td>";
                        _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentCommission[b].ToString() + "</td>";
                        _html += "</tr>";
                    }

                    _html += "<tr>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100;'><b>&nbsp;</b></td>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedPremiumTotal.ToString() + "</td>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100;'><b>&nbsp;</b></td>";
                    _html += "<td align='right' style='font-size:16px; background-color:#FFE100; mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentCommissionTotal.ToString() + "</td>";
                    _html += "</tr>";

                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Broker Commission Summary - End

                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table></td>";
                    }
                    else
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td></tr></table></td>";
                    }

                    //Report Details - Start
                    #region Report Details
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='4' style='font-size:20px; background-color:#D8D8D8;'><b>Report Details</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>User</b></td>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>" + Session[CE.Global.General.UserName].ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>Selected Month</b></td>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF; mso-number-format:mmm\\,yyyy'><b>" + GetReportMonth(txtReportDate.Text.Trim()) + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>Report Date / Time</b></td>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'><b>" + CE.Global.General.GetUTCDateTime() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Report Details - End

                    _html += "</tr>";
                    _html += "</table>";

                    _html += "<table width='20%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "</table>";

                    //Agent submission - Start
                    #region Agent submission
                    _html += "<table width='100%' cellpadding='1' cellspacing='1' border='1'>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<tr><td align='left' colspan='10' style='font-size:20px; background-color:#D8D8D8;'><b>Transaction Details</b></td></tr>";
                    }
                    else
                    {
                        _html += "<tr><td align='left' colspan='9' style='font-size:20px; background-color:#D8D8D8;'><b>Transaction Details</b></td></tr>";
                    }
                    _html += "<tr>";
                    _html += "<td width='120' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Application Date</b></td>";
                    _html += "<td width='100' align='left' style='font-size:14px; background-color:#81BEF7;'><b>QuoteRefNo</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Broker</b></td>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Customer Name</b></td>";
                    }
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Insurance Type</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Insurance Company</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>VMI</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>CMI</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>Total Premium</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>Collected Amount</b></td>";
                    _html += "</tr>";
                    for (int j = 0; j < _dsSummary.Tables[0].Rows.Count; j++)
                    {

                        _html += "<tr>";
                        _html += "<td width='120' align='left' style='background-color:#FBEFFB; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'>" + ApplicationDate[j].ToString() + "</td>";
                        _html += "<td width='100' align='left' style='background-color:#FBEFFB;'>" + QuoteRefNo[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + AgentName[j].ToString() + "</td>";
                        if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                        {
                            _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CustomerName[j].ToString() + "</td>";
                        }
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + InsuranceType[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CompanyName[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + VMI[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CMI[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + Premium[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedAmount[j].ToString() + "</td>";
                        _html += "</tr>";

                    }
                    _html += "<tr>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td colspan='9' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>Total Collected Amount</b></td>";
                    }
                    else
                    {
                        _html += "<td colspan='8' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>Total Collected Amount</b></td>";
                    }
                    _html += "<td width='100px' align='right' style='font-size:17px; background-color:#F5FBEF; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + TotalCollectedPremium.ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    #endregion
                    //Agent submission - End
                }
                _html += "</body>";
                _html += "</html>";


                lblContent.Text = _html;
                ExportGridView(_fileName, lblContent);
            }
        }
    }

    public void GenerateExcelOffline()
    {
        string _html = "";
        string _fileName = "AgentCommision@" + CE.Global.General.GetUTCDateTime().Day.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Month.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Year;
        lblContent.Text = "";
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }


            Int32 CommissionCount = 0;
            Int32 TotalFileCount = 0;
            DataSet _dsSummary = new DataSet();
            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate = txtReportDate.Text.Trim();
            Int64 AgentId = Convert.ToInt64(ddlAgentList.Text);
            Int32 _UserTypeId = Convert.ToInt32(ddlCampaign.Text);

            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "[sprocCXCRMCompletedApplicationSelectedList]";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@ReportDate", _ReportDate);
            _sPara.Add("@AgentId", AgentId);
            _sPara.Add("@UserTypeId", _UserTypeId);

            _dsSummary = _db.GetDataSet(_spName, _sPara);
            if (_dsSummary != null && _dsSummary.Tables.Count > 1 && _dsSummary.Tables[1].Rows.Count > 0)
            {
                TotalFileCount += _dsSummary.Tables[1].Rows.Count;
            }
            if (TotalFileCount > 0)
            {
                string[] ApplicationDate = new string[TotalFileCount];
                string[] QuoteRefNo = new string[TotalFileCount];
                string[] AgentName = new string[TotalFileCount];
                string[] CustomerName = new string[TotalFileCount];
                string[] InsuranceType = new string[TotalFileCount];
                string[] CompanyName = new string[TotalFileCount];
                string[] VMI = new string[TotalFileCount];
                string[] CMI = new string[TotalFileCount];
                string[] Premium = new string[TotalFileCount];
                string[] CollectedAmount = new string[TotalFileCount];
                string[] AgentOffCommission = new string[TotalFileCount];
                float TotalCollectedPremium = 0.0f;
                float AgentCommissionTotal = 0.0f;


                string SCName, SCBank, CardNo, CardType, CardExpiry, Status, Commision, TotalCommision, PaymentType;
                SCName = ""; SCBank = ""; CardNo = ""; CardType = ""; CardExpiry = ""; Status = ""; Commision = ""; TotalCommision = "";
                PaymentType = "";
                for (int i = 0; i < TotalFileCount; i++)
                {
                    ApplicationDate[i] = Convert.ToDateTime(_dsSummary.Tables[1].Rows[i]["ApplicationDate"].ToString()).ToString("dd/MM/yyyy hh:mm tt");
                    QuoteRefNo[i] = _dsSummary.Tables[1].Rows[i]["QuoteRefNo"].ToString();
                    AgentName[i] = _dsSummary.Tables[1].Rows[i]["AgentName"].ToString();
                    CustomerName[i] = _dsSummary.Tables[1].Rows[i]["CustomerName"].ToString();
                    InsuranceType[i] = _dsSummary.Tables[1].Rows[i]["InsuranceType"].ToString();
                    CompanyName[i] = _dsSummary.Tables[1].Rows[i]["CompanyName"].ToString();
                    VMI[i] = _dsSummary.Tables[1].Rows[i]["VMI"].ToString();
                    CMI[i] = _dsSummary.Tables[1].Rows[i]["CMI"].ToString();
                    Premium[i] = _dsSummary.Tables[1].Rows[i]["TotalPremium"].ToString();
                    CollectedAmount[i] = _dsSummary.Tables[1].Rows[i]["CollectedAmount"].ToString();
                    AgentOffCommission[i] = _dsSummary.Tables[1].Rows[i]["Commission"].ToString();
                    TotalCollectedPremium += Convert.ToSingle(CollectedAmount[i].ToString());
                    AgentCommissionTotal += Convert.ToSingle(AgentOffCommission[i].ToString());
                }

                _html += "<html>";
                _html += "<head>";
                _html += "<meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
                _html += "</head>";
                _html += "<body>";

                if (ddlLanguage.Text == "1")
                {
                    _html += "<table width='100%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr>";

                    //Broker Commission Summary - Start
                    #region Broker Commission
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='4' style='font-size:20px; background-color:#D8D8D8;'><b>คอมมิชชั่นสำหรับโบรกเกอร์</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;' colspan='2'><b>โบรกเกอร์</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>จํานวนเงินที่เก็บรวบรวมโดยรวม</b></td>";
                     _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>คอมมิชชั่น</b></td>";
                    _html += "</tr>";
               
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2'>" + AgentName[0].ToString() + "</td>";
                    _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + TotalCollectedPremium.ToString() + "</td>";
                    _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentCommissionTotal.ToString() + "</td>";
                    _html += "</tr>";
             
                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Broker Commission Summary - End

                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table></td>";
                    }
                    else
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td></tr></table></td>";
                    }

                    //Report Details - Start
                    #region Report Details
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='5' style='font-size:20px; background-color:#D8D8D8;'><b>รายละเอียดรายงาน</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>ผู้ใช้งาน</b></td>";
                    _html += "<td align='left' colspan='3' style='font-size:14px; background-color:#F5FBEF;'><b>" + Session[CE.Global.General.UserName].ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>เดือนที่เลือก</b></td>";
                    _html += "<td align='left' colspan='3' style='font-size:14px; background-color:#F5FBEF; mso-number-format:mmm\\,yyyy'><b>" + GetReportMonth(txtReportDate.Text.Trim()) + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>รายงานวันที่ / เวลา</b></td>";
                    _html += "<td align='left' colspan='3' style='font-size:14px; background-color:#F5FBEF; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'><b>" + CE.Global.General.GetUTCDateTime() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Report Details - End

                    _html += "</tr>";
                    _html += "</table>";

                    _html += "<table width='20%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "</table>";

                    //Agent submission - Start
                    #region Agent submission
                    _html += "<table width='100%' cellpadding='1' cellspacing='1' border='1'>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<tr><td align='left' colspan='11' style='font-size:20px; background-color:#D8D8D8;'><b>รายละเอียดการทำรายการ</b></td></tr>";
                    }
                    else
                    {
                        _html += "<tr><td align='left' colspan='10' style='font-size:20px; background-color:#D8D8D8;'><b>รายละเอียดการทำรายการ</b></td></tr>";
                    }
                    _html += "<tr>";
                    _html += "<td width='120' align='left' style='font-size:14px; background-color:#81BEF7;'><b>วันที่สมัคร</b></td>";
                    _html += "<td width='100' align='left' style='font-size:14px; background-color:#81BEF7;'><b>หมายเลขอ้างอิง</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>โบรกเกอร์</b></td>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>ชื่อลูกค้า</b></td>";
                    }
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>ประเภทประกันภัย</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>บริษัทประกันภัย</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>ประกันภัยรถยนต์</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>พ.ร.บ</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>เบี้ยรวมทั้งหมด</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>จำนวนเงินที่รวบรวมได้</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>คอมมิชชั่น</b></td>";
                    _html += "</tr>";
                    for (int j = 0; j < _dsSummary.Tables[0].Rows.Count; j++)
                    {

                        _html += "<tr>";
                        _html += "<td width='120' align='left' style='background-color:#FBEFFB; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'>" + ApplicationDate[j].ToString() + "</td>";
                        _html += "<td width='100' align='left' style='background-color:#FBEFFB;'>" + QuoteRefNo[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + AgentName[j].ToString() + "</td>";
                        if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                        {
                            _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CustomerName[j].ToString() + "</td>";
                        }
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + InsuranceType[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CompanyName[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + VMI[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CMI[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + Premium[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedAmount[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentOffCommission[j].ToString() + "</td>";
                        _html += "</tr>";

                    }
                    _html += "<tr>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td colspan='9' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>จํานวนเงินที่เก็บรวบรวมโดยรวม</b></td>";
                    }
                    else
                    {
                        _html += "<td colspan='8' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>จํานวนเงินที่เก็บรวบรวมโดยรวม</b></td>";
                    }
                    _html += "<td width='100px' align='right' style='font-size:17px; background-color:#F5FBEF; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + TotalCollectedPremium.ToString() + "</b></td>";
                    _html += "<td width='100px' align='right' style='font-size:17px; background-color:#F5FBEF; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + AgentCommissionTotal.ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    #endregion
                    //Agent submission - End
                }
                else if (ddlLanguage.Text == "2")
                {
                    _html += "<table width='100%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr>";

                    //Broker Commission Summary - Start
                    #region Broker Commission
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='4' style='font-size:20px; background-color:#D8D8D8;'><b>Broker Commission</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;' colspan='2'><b>Broker</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>Total Collected Amount</b></td>";
                    _html += "<td align='center' style='font-size:14px; background-color:#cbffce;'><b>Commission</b></td>";
                    _html += "</tr>";

                    _html += "<tr>";
                    _html += "<td align='left' colspan='2'>" + AgentName[0].ToString() + "</td>";
                    _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + TotalCollectedPremium.ToString() + "</td>";
                    _html += "<td align='right' style='mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentCommissionTotal.ToString() + "</td>";
                    _html += "</tr>";
             
                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Broker Commission Summary - End

                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table></td>";
                    }
                    else
                    {
                        _html += "<td><table width='30%' cellpadding='1' cellspacing='1'><tr><td>&nbsp;</td></tr></table></td>";
                    }

                    //Report Details - Start
                    #region Report Details
                    _html += "<td>";
                    _html += "<table width='35%' cellpadding='1' cellspacing='1' border='1'>";
                    _html += "<tr><td align='left' colspan='5' style='font-size:20px; background-color:#D8D8D8;'><b>Report Details</b></td></tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>User</b></td>";
                    _html += "<td align='left' colspan='3' style='font-size:14px; background-color:#F5FBEF;'><b>" + Session[CE.Global.General.UserName].ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>Selected Month</b></td>";
                    _html += "<td align='left' colspan='3' style='font-size:14px; background-color:#F5FBEF; mso-number-format:mmm\\,yyyy'><b>" + GetReportMonth(txtReportDate.Text.Trim()) + "</b></td>";
                    _html += "</tr>";
                    _html += "<tr>";
                    _html += "<td align='left' colspan='2' style='font-size:14px; background-color:#F5FBEF;'><b>Report Date / Time</b></td>";
                    _html += "<td align='left' colspan='3' style='font-size:14px; background-color:#F5FBEF; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'><b>" + CE.Global.General.GetUTCDateTime() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    _html += "</td>";
                    #endregion
                    //Report Details - End

                    _html += "</tr>";
                    _html += "</table>";

                    _html += "<table width='20%' cellpadding='1' cellspacing='1'>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "<tr><td>&nbsp;</td></tr>";
                    _html += "</table>";

                    //Agent submission - Start
                    #region Agent submission
                    _html += "<table width='100%' cellpadding='1' cellspacing='1' border='1'>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<tr><td align='left' colspan='11' style='font-size:20px; background-color:#D8D8D8;'><b>Transaction Details</b></td></tr>";
                    }
                    else
                    {
                        _html += "<tr><td align='left' colspan='10' style='font-size:20px; background-color:#D8D8D8;'><b>Transaction Details</b></td></tr>";
                    }
                    _html += "<tr>";
                    _html += "<td width='120' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Application Date</b></td>";
                    _html += "<td width='100' align='left' style='font-size:14px; background-color:#81BEF7;'><b>QuoteRefNo</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Broker</b></td>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Customer Name</b></td>";
                    }
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Insurance Type</b></td>";
                    _html += "<td width='200' align='left' style='font-size:14px; background-color:#81BEF7;'><b>Insurance Company</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>VMI</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>CMI</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>Total Premium</b></td>";
                    _html += "<td width='130' align='right' style='font-size:14px; background-color:#81BEF7;'><b>Collected Amount</b></td>";
                    _html += "<td width='100' align='right' style='font-size:14px; background-color:#81BEF7;'><b>Commission</b></td>";
                    _html += "</tr>";
                    for (int j = 0; j < _dsSummary.Tables[0].Rows.Count; j++)
                    {

                        _html += "<tr>";
                        _html += "<td width='120' align='left' style='background-color:#FBEFFB; mso-number-format:dd\\-mmm\\-yyyy hh\\:mm tt'>" + ApplicationDate[j].ToString() + "</td>";
                        _html += "<td width='100' align='left' style='background-color:#FBEFFB;'>" + QuoteRefNo[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + AgentName[j].ToString() + "</td>";
                        if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                        {
                            _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CustomerName[j].ToString() + "</td>";
                        }
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + InsuranceType[j].ToString() + "</td>";
                        _html += "<td width='200' align='left' style='background-color:#FBEFFB;'>" + CompanyName[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + VMI[j].ToString() + "</td>";
                        _html += "<td width='100' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CMI[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + Premium[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + CollectedAmount[j].ToString() + "</td>";
                        _html += "<td width='130' align='right' style='background-color:#FBEFFB; mso-number-format:\\#\\,\\#\\#0\\.00'>" + AgentOffCommission[j].ToString() + "</td>";
                        _html += "</tr>";

                    }
                    _html += "<tr>";
                    if (Session[CE.Global.General.UserRole] != null && (Session[CE.Global.General.UserRole].ToString() == "300" || Session[CE.Global.General.UserRole].ToString() == "200"))
                    {
                        _html += "<td colspan='9' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>Total Collected Amount</b></td>";
                    }
                    else
                    {
                        _html += "<td colspan='8' align='right' style='font-size:17px; background-color:#F5FBEF;'><b>Total Collected Amount</b></td>";
                    }
                    _html += "<td width='100px' align='right' style='font-size:17px; background-color:#F5FBEF; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + TotalCollectedPremium.ToString() + "</b></td>";
                    _html += "<td width='100px' align='right' style='font-size:17px; background-color:#F5FBEF; mso-number-format:\\#\\,\\#\\#0\\.00'><b>" + AgentCommissionTotal.ToString() + "</b></td>";
                    _html += "</tr>";
                    _html += "</table>";
                    #endregion
                    //Agent submission - End
                }
                _html += "</body>";
                _html += "</html>";


                lblContent.Text = _html;
                ExportGridView(_fileName, lblContent);
            }
        }
    }

    public void ExportGridView(string fileName, Label lblContent)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        Response.Write(lblContent.Text);
        Response.End();
    }

    private void LoadAgentList()
    {
        try
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            DataTable _dt = new DataTable("tblList");
            //string _spName = "sprocCXCRMGetAgentList";
            string _spName = "sprocCXCRMGetBrokerList";

            _dt = _db.GetDataTable(_spName);
            ddlAgentList.Items.Clear();
            if (_dt != null && _dt.Rows.Count > 0)
            {
                ddlAgentList.DataSource = _dt;
                ddlAgentList.DataTextField = "FULLNAME";
                ddlAgentList.DataValueField = "CEID";
                ddlAgentList.DataBind();
                ddlAgentList.Items.Insert(0, new ListItem("All", "0"));
            }
        }
        catch (Exception Ex)
        {
        }
    }

    private void LoadAgentListByAgentId()
    {
        try
        {
            string Date = ddlYear1.Text + ddlMonth1.Text + "01";
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            DataTable _dt = new DataTable("tblList");
            string _spName = "sprocCXCRMGetBrokerList";
            SortedList _sPara = new SortedList();
            Int32 _UserTypeId = Convert.ToInt32(ddlCampaign.Text);
            _sPara.Add("@UserTypeId", _UserTypeId);
            _sPara.Add("@Date", Date);

            _dt = _db.GetDataTableBySP(_spName, _sPara);
            ddlAgentList.Items.Clear();
            if (_dt != null && _dt.Rows.Count > 0)
            {
                ddlAgentList.DataSource = _dt;
                ddlAgentList.DataTextField = "FULLNAME";
                ddlAgentList.DataValueField = "CEID";
                ddlAgentList.DataBind();
                ddlAgentList.Items.Insert(0, new ListItem("All", "0"));
            }
        }
        catch (Exception Ex)
        {
        }
    }

    private void LoadAgentListByDate()
    {
        try
        {
            string _ReportDate = "";
            _ReportDate = ddlYear1.Text;
            _ReportDate += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    _ReportDate += ddlDay1.Text;
                }
            }
            if (_ReportDate.Length == 6)
            {
                _ReportDate += _ReportDate.ToString() + "01";
            }
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            DataTable _dt = new DataTable("tblList");
            SortedList _sPara = new SortedList();
            string _spName = "sprocCXCRMGetBrokerList";
            _sPara.Add("@ReportDate", _ReportDate);

            _dt = _db.GetDataTableBySP(_spName, _sPara);
            ddlAgentList.Items.Clear();
            if (_dt != null && _dt.Rows.Count > 0)
            {
                ddlAgentList.DataSource = _dt;
                ddlAgentList.DataTextField = "FULLNAME";
                ddlAgentList.DataValueField = "CEID";
                ddlAgentList.DataBind();
            }
        }
        catch (Exception Ex)
        {
        }
    }

    public DataTable GetPercentage(string PremiumLevel, string ReportDate)
    {
        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        SortedList _sPara = new SortedList();
        string _spName = "sprocCXCRMAgentCommissionSelectSingleItem";
        _sPara.Add("@PremiumLevel", PremiumLevel);
        _sPara.Add("@ReportDate", ReportDate);
        DataTable _dtPercentage = new DataTable();
        _dtPercentage = _db.GetDataTableBySP(_spName, _sPara);
        return _dtPercentage;
    }

    public DataTable GetPercentage(string PremiumLevel, string ReportDate, float AgentCollected)
    {
        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        SortedList _sPara = new SortedList();
        string _spName = "sprocCXCRMAgentCommissionSelectSingleItem";
        _sPara.Add("@PremiumLevel", PremiumLevel);
        _sPara.Add("@ReportDate", ReportDate);
        _sPara.Add("@AgentCollected", AgentCollected);
        DataTable _dtPercentage = new DataTable();
        _dtPercentage = _db.GetDataTableBySP(_spName, _sPara);
        return _dtPercentage;
    }

    public string GetReportMonth(string ReportMonth)
    {
        string month = ReportMonth.Substring(4, 2);
        string year = ReportMonth.Substring(0, 4);
        string result = "";
        if (month == "01")
        {
            result = " Jan ";
        }
        else if (month == "02")
        {
            result = " Feb ";
        }
        else if (month == "03")
        {
            result = " Mar ";
        }
        else if (month == "04")
        {
            result = " Apr ";
        }
        else if (month == "05")
        {
            result = " May ";
        }
        else if (month == "06")
        {
            result = " Jun ";
        }
        else if (month == "07")
        {
            result = " Jul ";
        }
        else if (month == "08")
        {
            result = " Aug ";
        }
        else if (month == "09")
        {
            result = " Sep ";
        }
        else if (month == "10")
        {
            result = " Oct ";
        }
        else if (month == "11")
        {
            result = " Nov ";
        }
        else if (month == "12")
        {
            result = " Dec ";
        }
        return result + year;
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnDownLoad.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnDownLoad.Enabled = true;
        }
    }

    public void ExportGridView(string fileName, GridView gvExport, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;
        gvExport.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(gvExport);
        frm.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

}
